package com.example.imchat.model.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.imchat.model.bean.UserInfo;
import com.example.imchat.model.db.DBHelper;

import java.util.ArrayList;
import java.util.List;

/*
* 联系人表的操作类
* */
public class ContactTableDao {
    private DBHelper mHelper;

    public ContactTableDao(DBHelper Helper) {
        this.mHelper = Helper;
    }
    //获取所有联系人
    public List<UserInfo> getContacts(){
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();

        //执行语句

        String sql = "select * from " + ContactTable.TAB_NAME + " where " + ContactTable.COL_IS_CONTACT + "=1";
        Cursor cursor = db.rawQuery(sql, null);

        List<UserInfo> users = new ArrayList<>();   //多个联系人集合
        while(cursor.moveToNext()){
            UserInfo userInfo = new UserInfo();

            userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
            userInfo.setName(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)));
            userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
            userInfo.setavatar(cursor.getString(cursor.getColumnIndex(ContactTable.COL_AVATAR)));
            users.add(userInfo);
        }
        //关闭资源
            cursor.close();
        //返回数据
        return users;
    }

    //通过环信id获取单个信息
    public UserInfo getContactByHxid(String hxid){
        if (hxid == null){
            return  null;
        }
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行语句
        String sql = " select * from " + ContactTable.TAB_NAME + " where " + ContactTable.COL_HXID + "=?";
        Cursor cursor = db.rawQuery(sql,new String[]{hxid});

                UserInfo userInfo = null;
            if (cursor.moveToNext()){
                userInfo = new UserInfo();

                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)));
                userInfo.setavatar(cursor.getString(cursor.getColumnIndex(ContactTable.COL_AVATAR)));
            }
                //关闭资源
                cursor.close();
                //返回数据
                return userInfo;
    }

    //通过环信id获取用户联系人信息
    public List<UserInfo> getContactsByHxid(List<String> hxids){
        if (hxids == null ||hxids.size() <= 0){
            return  null;
        }

        List<UserInfo> contacts = new ArrayList<>();
        //遍历环信id，查找
        for(String hxid:hxids){
            UserInfo contact = getContactByHxid(hxid);
            contacts.add(contact);
        }
        //返回数据
        return  contacts;
    }

    //保存单个联系人
    public void saveContact(UserInfo user , boolean isMyContact){

        if(user == null){
            return ;
        }
        //获取链接
        SQLiteDatabase db = mHelper.getReadableDatabase();
        //执行语句
        ContentValues values = new ContentValues();
        values.put( ContactTable.COL_HXID, user.getHxid());
        values.put( ContactTable.COL_NAME, user.getName());
        values.put( ContactTable.COL_NICK, user.getNick());
        values.put( ContactTable.COL_AVATAR, user.getavatar());
        values.put( ContactTable.COL_IS_CONTACT, isMyContact ? 1 :0);

        db.replace(ContactTable.TAB_NAME,null,values);

    }

    //保存联系人信息
    public void saveContacts(List<UserInfo> contacts,boolean isMyContact){
            if (contacts ==null || contacts.size()<=0){
                return;
            }
            for (UserInfo contact :contacts){
                saveContact(contact ,isMyContact);
            }
    }
    //删除联系人信息
    public void deleteContactByHxid(String hxid){
        if (hxid ==null){
            return;
        }
        SQLiteDatabase db = mHelper.getReadableDatabase();

        db.delete(ContactTable.TAB_NAME, ContactTable.COL_HXID+" =?",new String[]{hxid});

    }
}
